package com.edu.converter;
/*
 * @Author:princess
 * @Date 2023/11/22 11 22 18:58
 *
 */

import com.edu.bean.User;
import org.springframework.core.convert.converter.Converter;

/**
 *  1.自定义类型转换器 实现 Converter接口
 *  2.重写 convert 方法 定义我们自己的转换规则
 *  3.Spring容器中 配置我们自己的转换器
 *  4. <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
 */
public class MyStringToUserConverter implements Converter<String, User> {

    @Override
    public User convert(String source) {
        User user = null;
        try {
            String[] strings = source.split("-");
            int userId = Integer.parseInt(strings[0]);
            String username = strings[1];
            String password = strings[2];
            int age = Integer.parseInt(strings[3]);
            user = new User(userId,username,password,age);
        }catch (Exception e){
            e.printStackTrace();
        }
        return user;
    }
}
